using System.Linq;

public partial class NoTargetSkill : BaseSkill
{
    static RobotManager RobotManager => GameManager.Instance.LevelManager.RobotManager;
    static VFXManager VFXManager => GameManager.Instance.LevelManager.VFXManager;
    static BaseMapManager MapManager => GameManager.Instance.LevelManager.MapManager;
    static Cursor Cursor => GameManager.Instance.LevelManager.Cursor;

    public override async void Cast(SkillEventEntity e = null)
    {
        base.Cast(e);
        e.Targets = new();
        // 根据允许的目标类型，获取目标列表
        var type = e.Skill.AllowTarget;
        switch (type)
        {
            case SkillAllowTarget.Ally:
            case SkillAllowTarget.Self:
                var list = e.Skill.OwnerRobot.IsEnemy ? RobotManager.EnemyRobots : RobotManager.PlayerRobots.Concat(RobotManager.AllyRobots).ToList();
                foreach (var robot in list)
                {
                    if (e.Skill.OwnerRobot.DistanceTo(robot, MapManager) <= e.Skill.Radius)
                    {
                        e.Targets.Add(robot);
                    }
                }
                if (e.Skill.SkillData.ContainsKey("AddPercentHp"))
                {
                    var percent = (int)e.Skill.SkillData["AddPercentHp"];
                    foreach (var robot in e.Targets)
                    {
                        var amount = (int)(robot.Entity.TotalHealth * percent / 100f);
                        robot.TakeHeal(amount);
                        Cursor.LookAt(robot);
                        VFXManager.VFXWeaponRepair(robot.Position, amount);
                        await ToSignal(VFXManager, nameof(VFXManager.OnVFXEnd));
                    }
                }
                // buff
                if (e.Skill.BuffsConfig.Count > 0)
                {
                    foreach (var id in e.Skill.BuffsConfig)
                    {
                        foreach (var robot in e.Targets)
                        {
                            BuffEntity.AddBuff(robot, id);
                            VFXManager.VFXBlink(robot.Position);
                            await ToSignal(VFXManager, nameof(VFXManager.OnVFXEnd));
                        }
                    }
                }

                e.Targets = list;
                break;
        }

        EmitSignal(SignalName.OnCastEnd, e);
    }

    public NoTargetSkill(SkillModel md)
    {
        GameUtil.FastAssignment(md, this);
    }
}